package com.ruoyi.cngj.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cngj.domain.CngjTzj;
import com.ruoyi.cngj.service.ICngjTzjService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 探站记Controller
 * 
 * @author yuexl
 * @date 2024-06-27
 */
@Controller
@RequestMapping("/cngj/tzj")
public class CngjTzjController extends BaseController
{
    private String prefix = "cngj/tzj";

    @Autowired
    private ICngjTzjService cngjTzjService;

    @RequiresPermissions("cngj:tzj:view")
    @GetMapping()
    public String tzj()
    {
        return prefix + "/tzj";
    }

    /**
     * 查询探站记列表
     */
    //@RequiresPermissions("cngj:tzj:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CngjTzj cngjTzj)
    {
        startPage();
        List<CngjTzj> list = cngjTzjService.selectCngjTzjList(cngjTzj);
        return getDataTable(list);
    }

    /**
     * 导出探站记列表
     */
    //@RequiresPermissions("cngj:tzj:export")
    @Log(title = "探站记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CngjTzj cngjTzj)
    {
        List<CngjTzj> list = cngjTzjService.selectCngjTzjList(cngjTzj);
        ExcelUtil<CngjTzj> util = new ExcelUtil<CngjTzj>(CngjTzj.class);
        return util.exportExcel(list, "探站记数据");
    }

    /**
     * 新增探站记
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存探站记
     */
    //@RequiresPermissions("cngj:tzj:add")
    @Log(title = "探站记", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CngjTzj cngjTzj)
    {
        return toAjax(cngjTzjService.insertCngjTzj(cngjTzj));
    }

    /**
     * 修改探站记
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CngjTzj cngjTzj = cngjTzjService.selectCngjTzjById(id);
        mmap.put("cngjTzj", cngjTzj);
        return prefix + "/edit";
    }

    /**
     * 修改保存探站记
     */
    //@RequiresPermissions("cngj:tzj:edit")
    @Log(title = "探站记", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CngjTzj cngjTzj)
    {
        return toAjax(cngjTzjService.updateCngjTzj(cngjTzj));
    }

    /**
     * 删除探站记
     */
    //@RequiresPermissions("cngj:tzj:remove")
    @Log(title = "探站记", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(cngjTzjService.deleteCngjTzjByIds(ids));
    }

    /**
     * 改变位置
     */
    @Transactional
    @PostMapping("/tzjMove")
    @ResponseBody
    public AjaxResult tzjMove(String type,String id,String changeId)
    {
        CngjTzj cngjTzj = cngjTzjService.selectCngjTzjById(id);
        CngjTzj changeCngiTzj = cngjTzjService.selectCngjTzjById(changeId);
        int num =0;
        if("1".equals(type) || "2".equals(type)){
            //上移
            int currSort = cngjTzj.getTzjSort();
            cngjTzj.setTzjSort(changeCngiTzj.getTzjSort());
            changeCngiTzj.setTzjSort(currSort);
            num +=cngjTzjService.updateCngjTzj(cngjTzj);
            num +=cngjTzjService.updateCngjTzj(changeCngiTzj);
        }
        if("3".equals(type)){
            cngjTzj.setTzjSort(Integer.parseInt(changeId)+1);
            num +=cngjTzjService.updateCngjTzj(cngjTzj);
        }
        return toAjax(num);
    }

    /**
     * 启用停用探站记素材
     */
    @Log(title = "启用停用探站记素材")
    @PostMapping( "/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(String id,String tzjState)
    {
        CngjTzj cngjTzj = new CngjTzj();
        cngjTzj.setId(id);
        cngjTzj.setTzjState(tzjState);
        return toAjax(cngjTzjService.updateCngjTzj(cngjTzj));
    }

}
